#include "report_config.h"

u32 rec_message; //回报上报消息包
static u32 message; //自计算消息包
u16 send_num=0; //发送数据长度
u8 buffer[SIZE]; //临时存储空间

static u8 buffer_send[SIZE];


/*主模式变换上报*/
void change_main_mode_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*场景模式子模式变换上报*/
void change_scene_mode_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*CW 色温调节*/
void change_CW_level_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
} 

/*CW 亮度调节*/
void change_CW_brighness_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*分段模式*/
void change_part_info_handle(u32 code,PART_INFO_T part_info)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = part_info.len; 
    send_num +=1;
    memcpy(&buffer[send_num],part_info.RGB,45);
    send_num +=45;
}

/*麦克风灵敏度*/
void change_micro_senst_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}


/*麦克风子模式*/
void change_spectral_mode_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}

/*方向设置*/
void change_direction_mode_handle(u32 code,u8 num)
{
    memcpy(&buffer[send_num],&code,sizeof(u32));
    send_num +=4;
    buffer[send_num] = num;
    send_num +=1;
}



/*****************心跳包*************************/
void heart_beat_handle()
{
    /*
    u8 heart_buffer[SIZE];
    u16 j=0;
    memcpy(&heart_buffer[1+2],buf_device,16);  //DN
    j +=16;
    //u32 mess = get_message();
    memcpy(&heart_buffer[1+16+2],&mess,4);
    j +=4;
    //u32 code = HEARTBEAT_PACKET;
    memcpy(&heart_buffer[1+16+2+4],&code,4);
    j +=4;
    memcpy(&heart_buffer[1],&j,sizeof(u16));

    j+= sizeof(u16);
    heart_buffer[0]= 0xA5;
    j +=1;
    heart_buffer[j] =0;
    j +=1;
    heart_buffer[j] =0;
    j +=1;
    heart_buffer[j] =0x5A;
    j +=1;
   //TCP_send_data(heart_buffer,j);
   */
}

/******************回包用的messageId************************/
void set_rec_message(u8 *buf)
{
    memcpy(&rec_message,buf,4);
}

u32 get_rec_message()
{
    u32 i;
    i=rec_message;
    rec_message=0;
    return i;
}


/******************主动上报用的messageId******************************************/
u32 get_message()
{
    u32 i;
    if (message == 0xffffffff)
    {
        message =0;
    }
    i=message;
    message++;
    return i;
}


/*mac地址获取函数*/
void get_mac(u8 *buf)
{
    u8 mac[6];
    esp_err_t err = esp_read_mac(mac, ESP_MAC_WIFI_STA);
    if (ESP_OK == err)
    {
        char mac_str[18];
        snprintf(mac_str, sizeof(mac_str), "%02X:%02X:%02X:%02X:%02X:%02X",
        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        ESP_LOGI("MAC_INFO", "STA MAC Address: %s", mac_str);
    }
    else
    {
        ESP_LOGE("MAC_ERROR", "Failed to get STA MAC address. Error: %d", err);
    }
    memcpy(buf,mac,6*sizeof(uint8_t));
}

extern char buf_device[17];

/*上报注册包*/
void send_register_package()
{
    u16 num=0;
    u8 buf[1024];
    buf[0]=0xA5;
    memcpy(&buf[3],buf_device,16);  //deviceId
    num+=16;
    //MI
    u32 mess=get_message();
    memcpy(&buf[19],&mess,4); //messageId
    num+=4;
    //指令码
    memset(&buf[23],0,4);
    num+=4;
    //版本
    memcpy(&buf[27],SOFT_VER,3); //软件版本号
    num+=3;
    memcpy(&buf[30],FW_VER,3); //硬件版本号
    num+=3;
    memcpy(&buf[33],PROJECT_NAME,4); //PN
    num+=4;
    //硬件mac地址
    get_mac(&buf[37]);
    num+=6;
    //密钥
    memcpy(&buf[45],SECRET,strlen(SECRET)); //密钥
    num+=strlen(SECRET);
    u16 n =strlen(SECRET);
    memcpy(&buf[43],&n,2);
    num+=2;
    memcpy(&buf[1],&num,2);
    //CRC
    buf[51]=0;
    buf[52]=0;

    buf[53]=0x5a;

    uart1_msg_send(buf,54);
}

void report_dazzling_handle()
{
    if (0 != send_num)
    {
        int i=0;
        buffer_send[0]= 0xA5;
        i +=1;
        //DI
        memcpy(&buffer_send[1+2],buf_device,16);
        send_num+=16;
        i+=16;
        //MI
        u32 mess;
        mess = get_message();
        memcpy(&buffer_send[1+16+2],&mess,4);
        send_num +=4;
        i +=4;
        memcpy(&buffer_send[1],&send_num,sizeof(u16));
        i+= sizeof(u16);
        memcpy(&buffer_send[i],buffer,(send_num-4-16));
        i+=(send_num-4-16);
        buffer_send[i] =0;
        i +=1;
        buffer_send[i] =0;
        i +=1;
        buffer_send[i] =0x5A;
        i +=1;
        
        uart1_msg_send(buffer_send,i); //串口发送
        memset(buffer,0,send_num);
        memset(buffer_send,0,i);
        i=0;
        send_num = 0;
    }
}



